2024-09-09
最后编辑于:2024-09-15
void Pipeline::Rasterize(i32 minX, i32 minY, i32 maxX, i32 maxY, Ref<Image> image)
{
ZoneScopedN("Rasterization");
for (i32 ry = minY; ry < maxY; ++ry)
{
for (i32 rx = minX; rx < maxX; ++rx)
{
// 光栅化逻辑(与原始代码相同)
Vec2 p = Vec2((f32)rx + 0.5f, (f32)ry + 0.5f);
Vec2 v0 = m_VertexView[1].spf - m_VertexView[0].spf;
Vec2 v1 = m_VertexView[2].spf - m_VertexView[0].spf;
Vec2 v2 = p - m_VertexView[0].spf;
f32 dot00 = Dot(v0, v0);
f32 dot01 = Dot(v0, v1);
f32 dot11 = Dot(v1, v1);
f32 dot20 = Dot(v2, v0);
f32 dot21 = Dot(v2, v1);
f32 recipDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
f32 b = (dot11 * dot20 - dot01 * dot21) * recipDenom;
f32 c = (dot00 * dot21 - dot01 * dot20) * recipDenom;
f32 a = 1.0f - b - c;
if (a > 0.0f && b > 0.0f && c > 0.0f)
{
Varyings varyings;
for (usize i = 0; i < MaxVaryingNum; ++i)
{
varyings.vec2[i] = m_VertexView[0].varyings.vec2[i] * a +
m_VertexView[1].varyings.vec2[i] * b +
m_VertexView[2].varyings.vec2[i] * c;
varyings.vec3[i] = m_VertexView[0].varyings.vec3[i] * a +
m_VertexView[1].varyings.vec3[i] * b +
m_VertexView[2].varyings.vec3[i] * c;
varyings.vec4[i] = m_VertexView[0].varyings.vec4[i] * a +
m_VertexView[1].varyings.vec4[i] * b +
m_VertexView[2].varyings.vec4[i] * c;
}
Vec4 color = m_PixelShading(varyings);
u32 out =
u32(color.x * 255) << 24 |
u32(color.y * 255) << 16 |
u32(color.z * 255) << 8 |
u32(color.w * 255);
image->Data()[ry * image->Width() + rx] = out;
}
}
}
}